package com.api.controller;

import com.api.entity.Mailbox;
import com.api.entity.Notices;
import com.api.log.Log;
import com.api.mapper.MailboxMapper;
import com.api.utils.ParamsUtils;
import com.api.utils.Result;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

import static com.api.entity.table.MailboxTableDef.MAILBOX;
import static com.api.entity.table.NoticesTableDef.NOTICES;
import static com.api.entity.table.UserTableDef.USER;

/**
 * @Author: yangsf
 * @CreateDate: 2024/10/25
 */

@Slf4j
@RestController
@RequestMapping("/mailbox")
public class MailboxController extends BaseController {

    @Autowired
    MailboxMapper mailboxMapper;


    @Log("获取信箱列表")
    @RequestMapping("/getList")
    public Result getList(@RequestBody Map<String,Object> params){
        QueryWrapper qw = new QueryWrapper();
        qw.select(MAILBOX.ID,MAILBOX.TITLE,MAILBOX.STATUS,MAILBOX.CREATE_TIME).where(MAILBOX.STATUS.gt(0));
        if(params.containsKey("nameKeyword") && null != params.get("nameKeyword") && !StringUtils.isEmpty(params.get("nameKeyword"))){
            String nameKeyword = params.get("nameKeyword").toString();
            qw.and(MAILBOX.TITLE.like(nameKeyword));
        }
        if(params.containsKey("status") && null != params.get("status") && !StringUtils.isEmpty(params.get("status"))){
            Integer status = Integer.parseInt(params.get("status").toString());
            qw.and(MAILBOX.STATUS.eq(status));
        }
        qw.orderBy(MAILBOX.ID.desc());
        Page page = ParamsUtils.toPage(params);
        Page<Notices> pages =mailboxMapper.paginate(page,qw);
        return Result.ok(pages);
    }

    @Log("获取我的信箱列表")
    @RequestMapping("/getMailboxs")
    public Result getMailboxs(@RequestBody Map<String,Object> params){
        QueryWrapper qw = new QueryWrapper();

        qw.select(MAILBOX.ID,MAILBOX.TITLE,MAILBOX.STATUS,MAILBOX.CREATE_TIME)
        .where(MAILBOX.STATUS.gt(0))
        .and(MAILBOX.USER_ID.eq(getUserId()));

        if(params.containsKey("nameKeyword") && null != params.get("nameKeyword") && !StringUtils.isEmpty(params.get("nameKeyword"))){
            String nameKeyword = params.get("nameKeyword").toString();
            qw.and(MAILBOX.TITLE.like(nameKeyword));
        }
        if(params.containsKey("status") && null != params.get("status") && !StringUtils.isEmpty(params.get("status"))){
            Integer status = Integer.parseInt(params.get("status").toString());
            qw.and(MAILBOX.STATUS.eq(status));
        }
        qw.orderBy(MAILBOX.ID.desc());
        Page page = ParamsUtils.toPage(params);
        Page<Notices> pages =mailboxMapper.paginate(page,qw);
        return Result.ok(pages);
    }


    @Log("查看邮件")
    @RequestMapping("/get")
    public Result get(Long id){
        if(null == id){
            return Result.err("未获取到邮件信息");
        }
        Mailbox mailbox = Mailbox.create().setId(id).oneById();
        if(null == mailbox){
            return Result.err("未获取到邮件信息");
        }
        if(null == mailbox.getKind()){
            return Result.err("当前消息类型异常");
        }
        if(null == mailbox.getRellId()){
            return Result.err("未获取到通知信息或信息已被删除");
        }
        Notices notices = Notices.create().setId(mailbox.getRellId()).setStatus(2).oneById();
        if(null== notices){
            return Result.err("未获取到通知信息或信息已被删除");
        }
        //更新阅读状态
        mailbox.setStatus(2).updateById();
        return Result.ok(notices);
    }
}
